package com.swipemaster.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.swipemaster.R;
import com.swipemaster.achievements.AchievementsManager;
import com.swipemaster.achievements.LevelsInfo;
import com.swipemaster.common.Constants;

public class PlayLevelsActivity extends Activity
{
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.level_page);
		
		GridView gridview = (GridView) findViewById(R.id.grid_view);
		LevelsInfo levelsInfo = AchievementsManager.getLevelsInfo(this);
		gridview.setAdapter(new LevelSelectorAdapter(this, levelsInfo));
		gridview.setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				// TODO: Action after clicking on level selector.
			}
		});
	}

	private class LevelSelectorAdapter extends BaseAdapter
	{
		private Activity mActivity;
		private LevelsInfo mLevelsInfo;

		public LevelSelectorAdapter(Activity activity, LevelsInfo levelsInfo)
		{
			mActivity = activity;
			mLevelsInfo = levelsInfo;
		}

		public Object getItem(int position)
		{
			return null;
		}

		public long getItemId(int position)
		{
			return 0;
		}

		/**
		 * Create a new RelativeLayout with TextView and Image for each item referenced by the Adapter.
		 */
		public View getView(int position, View convertView, ViewGroup parent)
		{
			RelativeLayout layout;
			if (convertView == null)
			{
				// If it's not recycled, initialize some attributes.
				LayoutInflater inflater = LayoutInflater.from(mActivity.getApplicationContext());
				layout = (RelativeLayout) inflater.inflate(R.layout.level_selector, null);
			}
			else
			{
				layout = (RelativeLayout) convertView;
			}
			setLevelNumber(layout, position);
			setStarsImage(layout, position);
			return layout;
		}

		private void setLevelNumber(RelativeLayout relativeLayout, int position)
		{
			TextView textView = (TextView) relativeLayout.findViewById(R.id.levelNumber);
			boolean locked = mLevelsInfo.getInfo(position).isLocked();
			if (locked)
			{
				textView.setBackgroundResource(android.R.drawable.ic_lock_lock);
				textView.setText("");
			}
			else
			{
				textView.setBackgroundResource(0);
				textView.setText(position + 1 + "");
			}
		}

		private void setStarsImage(RelativeLayout relativeLayout, int position)
		{
			ImageView imageView = (ImageView) relativeLayout.findViewById(R.id.stars);
			int stars = mLevelsInfo.getInfo(position).getStars();
			imageView.setImageResource(Constants.getStarsResource(stars));
		}

		@Override
		public int getCount()
		{
			return Constants.LEVELS;
		}
	}

}